
const fetchSev = (url, method, data, fn) => {
  // 从storage中获取ticket
  let ticket = wx.getStorageSync("ticket");
  // console.log('cookie-ticket', ticket);
  if (ticket) {
    wx.request({
      url: url,
      method: method,
      data: data,
      header: {
        'content-type': 'application/json',
        'cookie': "PHPSESSID=" + ticket  // 同步传递cookie = ticket
      },
      success: function (res) {
        // console.log('调接口=>成功', res);
        if (res.data.retCode === 0) {
          fn(res.data.retData);
        } else {
          console.log("retCode !== 0", res);
        }
      },
      fail: function (err) {
        console.log('调接口=>失败', err);
      },
      complete: function () {
        // console.log('调接口=>完成');
      }
    });
  } else {
    console.log('Storage获取ticket失败');
  }
  
}


const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime,
  fetchSev: fetchSev
}
